<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of VideolFile
 *
 * @author ZangGuoPing
 */
class VideoFiles extends CActiveRecord {

    public $video_id;
    public $type;
    public $urllist;
    public $prefix;
    public $start = 0;
    public $end = 0;
    public $extension = '.rmvb';
    public $url;
    public $name;

    public function rules() {
        return array(
            array('file_path,file_name,file_sort,video_id,file_play_mode', 'required', 'on' => 'insert'),
            array('server', 'required', 'message' => '请选择视频所在服务器'),
            array('file_path', 'length', 'max' => 250),
            array('extension,prefix', 'length', 'max' => 255),
            array('start,end', 'numerical', 'min' => 1, 'max' => 1000),
            array('fid', 'numerical'),
            array('start,end', 'required')
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{video_files}}';
    }

    public function attributeLabels() {
        return array(
            'file_path' => Yii::t('default', '文件路径'),
            'file_name' => Yii::t('default', '文件名称'),
            'file_sort' => Yii::t('default', '显示顺序'),
            'server_id' => Yii::t('default', '所在服务器'),
            'video_id' => Yii::t('default', '所属视频'),
            'file_play_mode' => Yii::t('default', '是否免费'),
            'files_add_title' => Yii::t('default', '视频地址完善'),
            'file_mode' => Yii::t('default', '模式'),
            'type' => Yii::t('default', '录入模式'),
            'urllist' => Yii::t('default', '录入模式'),
            'start' => Yii::t('default', '开始集数'),
            'end' => Yii::t('default', '结束集数'),
            'server' => Yii::t('default', '视频服务器'),
        );
    }

    /**
     * 地址录入模式
     * @return type 
     */
    public function getInputMode() {
        return array('默认模式', '文本列表');
    }

    /**
     * 服务器列表
     * @param type $values
     * @return string 
     */
    public function getServerList($values=array()) {
        $values = is_array($values) ? $values : array();
        $html = '';
        foreach (Server::model()->findAll('server_parent_id=0') as $value) {
            $options = array('value' => $value['server_id'], 'id' => 'server_' . $value['server_id']);
            $html.= '<li class="title">' . CHtml::checkBox('VideoFiles[server][]', in_array($value['server_id'], $values), $options) . CHtml::label($value['server_name'], 'video_play_server' . $value['server_id']) . '</li>' .
                    $this->getChildrenServer($value['server_id'], $values);
        }
        return $html;
    }

    /**
     * 读取服务器子列表
     * @param type $parentId
     * @param type $values
     * @return string 
     */
    protected function getChildrenServer($parentId, $values=array()) {
        $html = "";
        foreach (Server::model()->findAll('server_parent_id=:id', array(':id' => $parentId)) as $value) {
            $options = array('value' => $value['server_id'], 'id' => 'server_' . $value['server_id']);
            $html.= '<li>' . CHtml::checkBox('VideoFiles[server][]', in_array($value['server_id'], $values), $options) . CHtml::label($value['server_name'], 'video_play_server' . $value['server_id']) . '</li>';
        }
        return $html;
    }

    /**
     * 视频点播地址的授权类型
     * @param type $type
     * @return type 
     */
    public function getFilePlayMode() {
        return array('default' => Yii::t('default', '默认'), 'free' => Yii::t('default', '试看'), 'auth' => Yii::t('default', '收费'));
    }

    /**
     * 读取视频文件所在的服务器
     */
    public function getServer() {
        $server = VideoServerId::model();
        $list = $server->findAll('video_id=:id', array(':id' => (int) $this->video_id));
        if ($list) {
            foreach ($list as $var) {
                $this->server[] = $var['server_id'];
            }
        }
        unset($list, $server);
        return $this->server;
    }

    public function setServer($server) {

        $this->server = $server;
    }

    /**
     * 读取视频文件的地址
     * @return array
     */
    public function getFiles() {
        $cri= new CDbCriteria();
        $cri->addCondition('video_id=:vid');
        $cri->params[':vid'] = (int)$this->video_id;
        $cri->order = 'file_sort ASC';
        $files = $this->findAll($cri);
        $data = array();
        if($files){
            foreach($files as $var){
                array_push($data, $var);
            }
        }
        //手动生成
        if ($this->start < $this->end) {
            $i = $this->start;
            
            while ($i <= $this->end) {
                $list['file_path'] = $this->prefix . $i . $this->extension;
                if ($i < 10 && $this->end > 10) {
                    $list['file_name'] = empty($this->video_title) ? '0' . $i : $this->video_title;
                } else {
                    $list['file_name'] = empty($this->video_title) ? $i : $this->video_title;
                }

                $list['file_sort'] = $i;
                $list['video_id'] = $this->video_id;
                $list['file_play_mode'] = 'default';
                $list['file_mode'] = 'default';
                array_push($data, $list);
                $i++;
            }
        }
        //生成结束
        return $data;
    }

}

?>
